/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React from 'react';
import type { ActionType } from '@ant-design/pro-table';
import { message } from 'antd';
import request from '@/utils/request';
import ProForm, { ModalForm, ProFormTextArea } from '@ant-design/pro-form';
import { isFunction } from 'lodash';

interface RefuseModalProps {
  label?: string;
  name?: string;
  children: JSX.Element;
  tableRef?: ActionType | any;
  callback?: () => void;
  formData?: any;
  api: string;
}
const RefuseModal: React.FC<RefuseModalProps> = ({
  label = '',
  name = 'reason',
  api,
  callback,
  formData,
  children,
  tableRef,
}) => {
  return (
    <ModalForm
      title='温馨提示'
      trigger={children}
      width={600}
      modalProps={{ centered: true, destroyOnClose: true }}
      onFinish={async (values) => {
        const { code, msg } = await request(api, {
          method: 'POST',
          data: { ...formData, ...values },
        });
        if (code == 0) {
          message.success('操作成功');
          if (callback && isFunction(callback)) {
            callback();
          } else {
            tableRef.current?.reload();
          }
          return true;
        }
        message.error(msg);
        return false;
      }}
    >
      <ProForm.Group>
        <h3>{label}</h3>
        <ProFormTextArea
          width='xl'
          name={name}
          placeholder='请输入原因'
          rules={[{ required: true, message: '请输入原因' }]}
          allowClear
        />
      </ProForm.Group>
    </ModalForm>
  );
};

export default RefuseModal;
